//
//  TransitionHelper.m
//  Karaoke
//
//  Created by Jiang Haofeng on 12-8-5.
//  Copyright (c) 2012年 Jiang Haofeng. All rights reserved.
//

#import "TransitionHelper.h"

@implementation TransitionHelper
static TransitionHelper *_sharedInstance;

+ (TransitionHelper *)sharedInstance
{
    if (!_sharedInstance)
    {
        _sharedInstance = [[TransitionHelper alloc] init];
    }

    return _sharedInstance;
}

- (CATransition *)showTransition:(CFTimeInterval)time type:(NSString *)type subType:(NSString *)subType view:(UIView *)view
{
    CATransition *animation = [CATransition animation];

    animation.delegate = self;
    animation.duration = time;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
    animation.type = type;
    animation.subtype = subType;
    [[view layer] addAnimation:animation forKey:@"animation"];

    return animation;

    /*
     *   case 101:
     *   animation.type = kCATransitionFade;
     *   break;
     *   case 102:
     *   animation.type = kCATransitionPush;
     *   break;
     *   case 103:
     *   animation.type = kCATransitionReveal;
     *   break;
     *   case 104:
     *   animation.type = kCATransitionMoveIn;
     *   break;
     *   case 201:
     *   animation.type = @"cube";
     *   break;
     *   case 202:
     *   animation.type = @"suckEffect";
     *   break;
     *   case 203:
     *   animation.type = @"oglFlip";
     *   break;
     *   case 204:
     *   animation.type = @"rippleEffect";
     *   break;
     *   case 205:
     *   animation.type = @"pageCurl";
     *   break;
     *   case 206:
     *   animation.type = @"pageUnCurl";
     *   break;
     *   case 207:
     *   animation.type = @"cameraIrisHollowOpen";
     *   break;
     *   case 208:
     *   animation.type = @"cameraIrisHollowClose";
     */
}

- (void)pushAnimationDidStop
{}

- (void)pushViewController          :(UIViewController *)soureViewController
        destinationViewConyroller   :(UIViewController *)destinationViewConyroller
        animatedWithTransition      :(UIViewAnimationTransition)transition
{
    [soureViewController.navigationController pushViewController:destinationViewConyroller animated:NO];
    destinationViewConyroller.view.alpha = 0;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5f];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(pushAnimationDidStop)];
    [UIView setAnimationTransition:transition forView:soureViewController.view cache:YES];
    destinationViewConyroller.view.alpha = 1;
    [UIView commitAnimations];
}

- (void)popViewController:(UIViewController *)viewController
{
    //    viewController.navigationController
    UIViewController *topViewController = viewController.navigationController.parentViewController;

    topViewController.view.alpha = 0;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5f];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(pushAnimationDidStop)];
    [UIView setAnimationTransition:YES forView:viewController.view cache:YES];
    [viewController.navigationController popViewControllerAnimated:YES];
    topViewController.view.alpha = 1;
    [UIView commitAnimations];
}

- (UIViewController *)popViewControllerAnimatedWithTransition:(UIViewAnimationTransition)transition viewController:(UIViewController *)viewController
{
    UIViewController *poppedController = [viewController.navigationController popViewControllerAnimated:NO];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5f];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(pushAnimationDidStop)];
    [UIView setAnimationTransition:transition forView:viewController.view cache:NO];
    [UIView commitAnimations];

    return poppedController;
}

- (void)popToRootViewControllerWithTransition:(UIViewAnimationTransition)transition viewController:(UIViewController *)viewController
{
    [viewController.navigationController popToRootViewControllerAnimated:NO];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5f];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(pushAnimationDidStop)];
    [UIView setAnimationTransition:transition forView:viewController.view cache:NO];
    [UIView commitAnimations];

    //    return poppedController;
}

@end